Improved error handling to reflect the actual situation, added tests
authorInokentiy Babushkin <inokentiy.babushkin@googlemail.com>
Thu, 1 Dec 2016 23:03:49 +0000 (00:03 +0100)
committerInokentiy Babushkin <inokentiy.babushkin@googlemail.com>
Thu, 1 Dec 2016 23:03:49 +0000 (00:03 +0100)
* One of the tests still doesn't pass, this needs further investigation

src/cargo/ops/cargo_rustc/context.rs
tests/rustflags.rs

index 6be2bef024d0582b5777c29ac0301233389c0dc0..2ec275e71b3b3fa46358a57fc34f2dc3fd6f945b 100644 (file)
@@ -856,19 +856,37 @@ fn env_args(config: &Config,
     // Then the target.*.rustflags value
     let target = build_config.requested_target.as_ref().unwrap_or(&build_config.host_triple);
     let key = format!("target.{}.{}", target, name);
-    if let Some(args) = config.get_list(&key)? {
+    let list = config.get_list(&key);
+    if let Ok(Some(args)) = list {
         let args = args.val.into_iter().map(|a| a.0);
         return Ok(args.collect());
     }
+    let string = config.get_string(&key);
+    if let Ok(Some(arg)) = string {
+        return Ok(arg.val.split(' ').map(str::to_string).collect());
+    }
+    if list.is_err() && string.is_err() {
+        if let Some(value) = config.values()?.get(&key) {
+            return config.expected("list or string", &key, value.clone());
+        }
+    }
 
     // Then the build.rustflags value
     let key = format!("build.{}", name);
-    if let Some(args) = config.get_list(&key)? {
+    let list = config.get_list(&key);
+    if let Ok(Some(args)) = list {
         let args = args.val.into_iter().map(|a| a.0);
         return Ok(args.collect());
-    } else if let Some(arg) = config.get_string(&key)? {
+    }
+    let string = config.get_string(&key);
+    if let Ok(Some(arg)) = string {
         return Ok(arg.val.split(' ').map(str::to_string).collect());
     }
+    if list.is_err() && string.is_err() {
+        if let Some(value) = config.values()?.get(&key) {
+            return config.expected("list or string", &key, value.clone());
+        }
+    }
 
     Ok(Vec::new())
 }
index cc9266ac281a69863bfbbda4156e51bd11766459..1cf9e96cd14b1ee7c5dbdda3c83d55eb455ab43e 100644 (file)
@@ -949,3 +949,89 @@ fn target_rustflags_precedence() {
     assert_that(p.cargo("bench"),
                 execs().with_status(101));
 }
+
+#[test]
+fn target_rustflags_string_and_array_form1() {
+    let p1 = project("foo")
+        .file("Cargo.toml", r#"
+            [package]
+            name = "foo"
+            version = "0.0.1"
+        "#)
+        .file("src/lib.rs", "")
+        .file(".cargo/config", r#"
+            [build]
+            rustflags = ["--cfg", "foo"]
+            "#);
+    p1.build();
+
+    assert_that(p1.cargo("build").arg("-v"),
+        execs().with_status(0).with_stderr("\
+[COMPILING] foo v0.0.1 ([..])
+[RUNNING] `rustc [..] --cfg foo[..]`
+[FINISHED] debug [unoptimized + debuginfo] target(s) in [..]
+"));
+
+    let p2 = project("foo")
+        .file("Cargo.toml", r#"
+            [package]
+            name = "foo"
+            version = "0.0.1"
+        "#)
+        .file("src/lib.rs", "")
+        .file(".cargo/config", r#"
+            [build]
+            rustflags = "--cfg foo"
+            "#);
+    p2.build();
+
+    assert_that(p2.cargo("build").arg("-v"),
+        execs().with_status(0).with_stderr("\
+[COMPILING] foo v0.0.1 ([..])
+[RUNNING] `rustc [..] --cfg foo[..]`
+[FINISHED] debug [unoptimized + debuginfo] target(s) in [..]
+"));
+
+}
+
+#[test]
+fn target_rustflags_string_and_array_form2() {
+    let p1 = project("foo")
+        .file("Cargo.toml", &format!(r#"
+            [package]
+            name = "foo"
+            version = "0.0.1"
+
+            [target.{}]
+            rustflags = ["--cfg", "foo"]
+        "#, rustc_host()))
+        .file("src/lib.rs", "");
+    p1.build();
+
+    assert_that(p1.cargo("build").arg("-v"),
+        execs().with_status(0).with_stderr("\
+[COMPILING] foo v0.0.1 ([..])
+[RUNNING] `rustc [..] --cfg foo[..]`
+[FINISHED] debug [unoptimized + debuginfo] target(s) in [..]
+"));
+
+    let p2 = project("foo")
+        .file("Cargo.toml", &format!(r#"
+            [package]
+            name = "foo"
+            version = "0.0.1"
+
+            [target.{}]
+            rustflags = "--cfg foo"
+        "#, rustc_host()))
+        .file("src/lib.rs", "");
+    p2.build();
+
+    assert_that(p2.cargo("build").arg("-v"),
+        execs().with_status(0).with_stderr("\
+[COMPILING] foo v0.0.1 ([..])
+[RUNNING] `rustc [..] --cfg foo[..]`
+[FINISHED] debug [unoptimized + debuginfo] target(s) in [..]
+"));
+
+}